﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PDFLibNet;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;

namespace kiosk
{
    public partial class PdfViewer : Form
    {

        #region classMember

        private Int32 DataID;
        private PDFLibNet.PDFWrapper mPdfDoc;
        private FileStream fs;
        private bool drag = false;
        private Point startPoint;
        private Int32 mHScrollValue;
        private Int32 mVcrollValue;
        private Double mcurrVcrollValue;
        private Double mcurrHcrollValue;
       
        private DataBaseHandler handler;
        private Int32 pageNumber = -1;
        private TaskbarHide.FormState fm;
        private Byte m_DataOptArray;
       
        public delegate void RenderNotifyInvoker(int page, bool isCurrent);
        //
        #endregion



        # region Scroll Constant

        private const int WM_SCROLL = 276; // Horizontal scroll
        private const int WM_VSCROLL = 277; // Vertical scroll
        private const int WM_HSCROLL = 276;
        private const int SB_LINEUP = 1; // Scrolls one line up
        private const int SB_LINELEFT = 1;// Scrolls one cell left
        private const int SB_LINEDOWN = 1; // Scrolls one line down
        private const int SB_LINERIGHT = 1;// Scrolls one cell right
        private const int SB_PAGEUP = 2; // Scrolls one page up
        private const int SB_PAGELEFT = 2;// Scrolls one page left
        private const int SB_PAGEDOWN = 3; // Scrolls one page down
        private const int SB_PAGERIGTH = 3; // Scrolls one page right
        private const int SB_PAGETOP = 6; // Scrolls to the upper left
        private const int SB_LEFT = 1; // Scrolls to the left
        private const int SB_PAGEBOTTOM = 7; // Scrolls to the upper right
        private const int SB_RIGHT = 1; // Scrolls to the right
        private const int SB_ENDSCROLL = 8; // Ends scroll
        private const int SB_BOTTOM = 0;




        #endregion

        #region Dll Api calls

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern int SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam);


        #endregion

        #region PrivateNon
        private bool LoadFile(string filename, PDFLibNet.PDFWrapper pdfDoc)
        {
            try
            {
                if (fs != null)
                {
                    fs.Close();
                    fs = null;
                }
                //Does not supported by MuPDF.                
                //fs = new System.IO.FileStream(filename, System.IO.FileMode.Open);                
                //return pdfDoc.LoadPDF(fs);                
                bool bRet = pdfDoc.LoadPDF(filename);

                return bRet;
            }
            catch (System.Security.SecurityException)
            {
                return false;

            }
        }


        private void updateCofig()
        {

            try
            {

                this.m_DataOptArray = this.handler.getDataOpt(this.DataID);
                

            }
            catch (Exception ex)
            {
                MessageBox.Show("There was an error" + ex.Message);
            }



        }

        private void openPDF(string path)
        {
            this.mPdfDoc = new PDFLibNet.PDFWrapper();
           
            this.mPdfDoc.LoadPDF(path);
            //this.mPdfDoc.RenderNotifyFinished += new RenderNotifyFinishedHandler(mPdfDoc_RenderNotifyFinished);


        }

      

        

        

        private void refreshPage()
        {

            if ((m_DataOptArray & (Byte)RotateView.Default) == (Byte)RotateView.Default)
            {
                // The default view is set

                   if(this.pageNumber != -1)
                    this.mPdfDoc.CurrentPage = this.pageNumber;
                
                //this.mPdfDoc.CurrentPage = this.pageNumber;//[this.pageNumber.Count - 1];

                this.mPdfDoc.FitToWidth(this.pdfViewBox.Handle);



                this.mPdfDoc.RenderPage(this.pdfViewBox.Handle);

                this.mPdfDoc.CurrentX = 0;
                this.mPdfDoc.CurrentY = 0;

                this.mPdfDoc.ClientBounds = new Rectangle(0, 0, this.mPdfDoc.PageWidth, this.mPdfDoc.PageHeight);

                Bitmap _backbuffer = new Bitmap(this.mPdfDoc.PageWidth, this.mPdfDoc.PageHeight);

                using (Graphics g = Graphics.FromImage(_backbuffer))
                {
                    this.mPdfDoc.DrawPageHDC(g.GetHdc());
                    g.ReleaseHdc();
                }

                Bitmap bitmap = _backbuffer;


                //



                this.pdfViewBox.Image = bitmap;
                System.Console.WriteLine("Dedfault rotate");

            }

            else if ((m_DataOptArray & (Byte)RotateView.RotateBy90) == (Byte)RotateView.RotateBy90)
            {

                // rotate by 90 set

                if (this.pageNumber != -1)
                    this.mPdfDoc.CurrentPage = this.pageNumber;

                // this.mPdfDoc.CurrentPage = this.pageNumber;//[this.pageNumber.Count - 1];

                this.mPdfDoc.FitToWidth(this.pdfViewBox.Handle);


                this.mPdfDoc.RenderPage(this.pdfViewBox.Handle);

                this.mPdfDoc.CurrentX = 0;
                this.mPdfDoc.CurrentY = 0;
                this.mPdfDoc.ClientBounds = new Rectangle(0, 0, this.mPdfDoc.PageWidth, this.mPdfDoc.PageHeight);

                Bitmap _backbuffer = new Bitmap(this.mPdfDoc.PageWidth, this.mPdfDoc.PageHeight);

                using (Graphics g = Graphics.FromImage(_backbuffer))
                {
                    this.mPdfDoc.DrawPageHDC(g.GetHdc());
                    g.ReleaseHdc();
                }

                Bitmap bitmap = _backbuffer;




                bitmap.RotateFlip(RotateFlipType.Rotate90FlipNone);
                System.Console.WriteLine("rotate 90");
                this.pdfViewBox.Image = bitmap;
            }

            else if ((m_DataOptArray & (Byte)RotateView.RotateBy180) == (Byte)RotateView.RotateBy180)
            {
                // rotate by 180 set

                if (this.pageNumber != -1)
                    this.mPdfDoc.CurrentPage = this.pageNumber;


                //this.mPdfDoc.CurrentPage = this.pageNumber;//[this.pageNumber.Count - 1];

                this.mPdfDoc.FitToWidth(this.pdfViewBox.Handle);

                this.mPdfDoc.RenderPage(this.pdfViewBox.Handle);



                this.mPdfDoc.CurrentX = 0;
                this.mPdfDoc.CurrentY = 0;
                this.mPdfDoc.ClientBounds = new Rectangle(0, 0, this.mPdfDoc.PageWidth, this.mPdfDoc.PageHeight);

                Bitmap _backbuffer = new Bitmap(this.mPdfDoc.PageWidth, this.mPdfDoc.PageHeight);

                using (Graphics g = Graphics.FromImage(_backbuffer))
                {
                    this.mPdfDoc.DrawPageHDC(g.GetHdc());
                    g.ReleaseHdc();
                }

                Bitmap bitmap = _backbuffer;


                bitmap.RotateFlip(RotateFlipType.Rotate180FlipNone);
                this.pdfViewBox.Image = bitmap;
                System.Console.WriteLine("Dedfault rotate 180");

            }

            else if ((m_DataOptArray & (Byte)RotateView.RotateBy270) == (Byte)RotateView.RotateBy270)
            {
                // rotate by 270 is set
                // this.mPdfDoc.CurrentPage = this.pageNumber;//[this.pageNumber.Count - 1];  

                if (this.pageNumber != -1)
                    this.mPdfDoc.CurrentPage = this.pageNumber;


                this.mPdfDoc.FitToWidth(this.pdfViewBox.Handle);

                this.mPdfDoc.RenderPage(this.pdfViewBox.Handle);

                this.mPdfDoc.CurrentX = 0;
                this.mPdfDoc.CurrentY = 0;
                this.mPdfDoc.ClientBounds = new Rectangle(0, 0, this.mPdfDoc.PageWidth, this.mPdfDoc.PageHeight);

                Bitmap _backbuffer = new Bitmap(this.mPdfDoc.PageWidth, this.mPdfDoc.PageHeight);

                using (Graphics g = Graphics.FromImage(_backbuffer))
                {
                    this.mPdfDoc.DrawPageHDC(g.GetHdc());
                    g.ReleaseHdc();
                }

                Bitmap bitmap = _backbuffer;

                bitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
                this.pdfViewBox.Image = bitmap;
                System.Console.WriteLine("Dedfault rotate 270");


            }

            else if ((m_DataOptArray & (Byte)RotateView.RotateByFlipx) == (Byte)RotateView.RotateByFlipx)
            {
                // rotate by flip x is selected
                // this.mPdfDoc.CurrentPage = this.pageNumber;//[this.pageNumber.Count - 1];

                if (this.pageNumber != -1)
                    this.mPdfDoc.CurrentPage = this.pageNumber;


                this.mPdfDoc.FitToWidth(this.pdfViewBox.Handle);

                this.mPdfDoc.RenderPage(this.pdfViewBox.Handle);


                this.mPdfDoc.CurrentX = 0;
                this.mPdfDoc.CurrentY = 0;
                this.mPdfDoc.ClientBounds = new Rectangle(0, 0, this.mPdfDoc.PageWidth, this.mPdfDoc.PageHeight);

                Bitmap _backbuffer = new Bitmap(this.mPdfDoc.PageWidth, this.mPdfDoc.PageHeight);

                using (Graphics g = Graphics.FromImage(_backbuffer))
                {
                    this.mPdfDoc.DrawPageHDC(g.GetHdc());
                    g.ReleaseHdc();
                }

                Bitmap bitmap = _backbuffer;

                bitmap.RotateFlip(RotateFlipType.RotateNoneFlipX);
                this.pdfViewBox.Image = bitmap;
                System.Console.WriteLine("Dedfault rotate x");

            }

            else if ((m_DataOptArray & (Byte)RotateView.RotateByFlipy) == (Byte)RotateView.RotateByFlipy)
            {
                // rota by y is selected 

                if (this.pageNumber != -1)
                    this.mPdfDoc.CurrentPage = this.pageNumber;


                //this.mPdfDoc.CurrentPage = this.pageNumber;//[this.pageNumber.Count - 1];

                this.mPdfDoc.FitToWidth(this.pdfViewBox.Handle);

                this.mPdfDoc.RenderPage(this.pdfViewBox.Handle);


                this.mPdfDoc.CurrentX = 0;
                this.mPdfDoc.CurrentY = 0;
                this.mPdfDoc.ClientBounds = new Rectangle(0, 0, this.mPdfDoc.PageWidth, this.mPdfDoc.PageHeight);

                Bitmap _backbuffer = new Bitmap(this.mPdfDoc.PageWidth, this.mPdfDoc.PageHeight);

                using (Graphics g = Graphics.FromImage(_backbuffer))
                {
                    this.mPdfDoc.DrawPageHDC(g.GetHdc());
                    g.ReleaseHdc();
                }

                Bitmap bitmap = _backbuffer;



                bitmap.RotateFlip(RotateFlipType.RotateNoneFlipY);
                this.pdfViewBox.Image = bitmap;
                System.Console.WriteLine("Dedfault rotate y");

            }

            else if ((m_DataOptArray & (Byte)RotateView.RotateFlipxy) == (Byte)RotateView.RotateFlipxy)
            {
                // Rotate by xy is selected

                //this.mPdfDoc.CurrentPage = this.pageNumber;//[this.pageNumber.Count - 1];

                if (this.pageNumber != -1)
                    this.mPdfDoc.CurrentPage = this.pageNumber;



                this.mPdfDoc.FitToWidth(this.pdfViewBox.Handle);

                this.mPdfDoc.RenderPage(this.pdfViewBox.Handle);


                this.mPdfDoc.CurrentX = 0;
                this.mPdfDoc.CurrentY = 0;
                this.mPdfDoc.ClientBounds = new Rectangle(0, 0, this.mPdfDoc.PageWidth, this.mPdfDoc.PageHeight);

                Bitmap _backbuffer = new Bitmap(this.mPdfDoc.PageWidth, this.mPdfDoc.PageHeight);

                using (Graphics g = Graphics.FromImage(_backbuffer))
                {
                    this.mPdfDoc.DrawPageHDC(g.GetHdc());
                    g.ReleaseHdc();
                }

                Bitmap bitmap = _backbuffer;

                bitmap.RotateFlip(RotateFlipType.RotateNoneFlipXY);
                this.pdfViewBox.Image = bitmap;
                System.Console.WriteLine("Dedfault rotate xy");


            }

            //this.mPdfDoc.FitToWidth(this.pdfViewBox.Handle);
            //
            this.mHScrollValue = this.mPdfDoc.PageHeight;
            this.mVcrollValue = this.mPdfDoc.PageWidth;
            //this.pdfViewBox.VerticalScroll.Maximum = this.mVcrollValue;
            //this.pdfViewBox.HorizontalScroll.Maximum = this.mHScrollValue;
            this.vScrollBar.Maximum = this.pdfViewBox.VerticalScroll.Maximum;
            this.hScrollBar.Maximum = this.pdfViewBox.HorizontalScroll.Maximum;
            //this.m_WinrsetEvent.Set();
            this.pdfViewBox.Invalidate();
            //this.pdfViewBox.PictBox.Invalidate();


        }


       






        #endregion 


        #region processKey
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.F6)
            {
                this.TopMost = false;
                GeneralOpt.pdfDataChanged = false;
                GeneralOpt.pdfHeadingChanged = false;

                DataDisplayOption disopt = new DataDisplayOption(this.DataID);
                disopt.ShowDialog();

                if (GeneralOpt.pdfDataChanged)
                {
                    // new 
                    this.handler.getTxtData(this.DataID);
                    
                    //

                    this.pageNumber = 1;
                    openPDF("temp.pdf");
                    this.updateCofig();
                    this.refreshPage();
                    this.pdfViewBox.Invalidate();

                    PDFPage pg = this.mPdfDoc.Pages[1];
                    this.tPageViewer.TileSize = new Size(134, (int)(128 * pg.Height / pg.Width) + 10);
                    this.tPageViewer.BeginUpdate();
                    this.tPageViewer.Clear();
                    for (int i = 0; i < this.mPdfDoc.PageCount; ++i)
                        this.tPageViewer.Items.Add((i + 1).ToString());
                    this.tPageViewer.EndUpdate();

                    //
                    this.dataHeaderBtn.Text = this.handler.getDataHeading(this.DataID);
                    this.Refresh();
                }

                else if(GeneralOpt.pdfHeadingChanged)
                {
                    this.updateCofig();
                    //
                    this.refreshPage();
                
                
                }

                this.TopMost = true;
            }

            if (keyData == Keys.F1)
            {
                this.TopMost = false;
                AboutBox1 ab = new AboutBox1();
                ab.ShowDialog();
                this.TopMost = true;

            }


            if (keyData == Keys.F2)
            {
                this.fm.Restore(this);

            }


            if(keyData == Keys.F8)
            {

                GeneralOpt.pictIDChange = -1;
                this.TopMost = false;
                BackGroundImages bg = new BackGroundImages();
                bg.ShowDialog();
                if (GeneralOpt.pictIDChange != -1)
                {
                    MessageBox.Show("Please wait while for changes to take effect");
                    ApplicationCache.LoadBgImages();
                    this.Invalidate();
                    this.mainTLPanel.Invalidate();
                
                
                }
                this.TopMost = true;
            
            
            }



            return base.ProcessCmdKey(ref msg, keyData);

        }


        #endregion



        #region eventHandlers
        void vScrollBar_Scroll(object sender, ScrollEventArgs e)
        {

            if (e.NewValue == this.mVcrollValue)
                e.NewValue -= 1;
            this.pdfViewBox.VerticalScroll.Value = e.NewValue;


        }

        void hScrollBar_Scroll(object sender, ScrollEventArgs e)
        {

            if (e.NewValue == this.mHScrollValue)
                e.NewValue -= 1;
            this.pdfViewBox.HorizontalScroll.Value = e.NewValue;
             

            
        }

        void pdfViewBox_MouseMove(object sender, MouseEventArgs e)
        {
           
            if (this.drag)
            {


                Point p1 = new Point(e.X, e.Y);
                this.mcurrHcrollValue = this.pdfViewBox.HorizontalScroll.Value;
                this.mcurrVcrollValue = this.pdfViewBox.VerticalScroll.Value;
                if (p1.X < this.startPoint.X)
                {

                    this.mcurrHcrollValue -= .8;
                    if (this.mcurrHcrollValue > 0)
                        this.pdfViewBox.HorizontalScroll.Value = Convert.ToInt32(this.mcurrVcrollValue);


                    // going to the left side

                }
                if (p1.X > this.startPoint.X)
                {

                    this.mcurrHcrollValue += 100;
                    if (this.mcurrHcrollValue < this.mVcrollValue)
                        SendMessage(this.pdfViewBox.Handle, WM_HSCROLL, (System.IntPtr)SB_RIGHT, System.IntPtr.Zero);


                    // going to the right

                }

                if (p1.Y > this.startPoint.Y)
                {
                    this.mcurrVcrollValue += 100;
                    if (this.mcurrVcrollValue < this.mHScrollValue)
                        //this.pdfViewBox.VerticalScroll.Value = Convert.ToInt32(this.mcurrVcrollValue);
                        SendMessage(this.pdfViewBox.Handle, WM_VSCROLL, (System.IntPtr)SB_LINEDOWN, System.IntPtr.Zero);
                    // GOING down SB_LINEUP
                     

                }

                if (p1.Y < this.startPoint.Y)
                {

                    this.mcurrVcrollValue -= .8;
                    if (this.mcurrVcrollValue > 0)
                        this.pdfViewBox.VerticalScroll.Value = Convert.ToInt32(this.mcurrVcrollValue);



                    // going up 
                }

            }

        }


        void PictBox_MouseUp(object sender, MouseEventArgs e)
        {

            this.drag = false;

        }

        void PictBox_MouseDown(object sender, MouseEventArgs e)
        {
            
            this.drag = true;
            if (this.startPoint == null)
            {

                this.startPoint = new Point();
            }
            this.startPoint.X = e.X;
            this.startPoint.Y = e.Y;



        }


       

        void pg_RenderThumbnailFinishedInvoke(int page, bool bSuccesss)
        {
            if (!bSuccesss)
                this.tPageViewer.Invalidate();
            this.tPageViewer.Invalidate(this.tPageViewer.Items[page - 1].Bounds);
            //PDFPage pg = _pdfDoc.Pages[page];
            //Bitmap bmp = pg.LoadThumbnail(imageList1.ImageSize.Width, (int)(imageList1.ImageSize.Width * pg.Height / pg.Width));
        }
        void pg_RenderThumbnailFinished(int page, bool bSuccesss)
        {
            Invoke(new RenderNotifyFinishedHandler(pg_RenderThumbnailFinishedInvoke), page, bSuccesss);
        }

        void RenderNotifyFinished(int page, bool isCurrent)
        {
            //if (tabView.SelectedTab.Equals(this.tpvText) && !_pdfDoc.IsBusy)
            //    txtTextView.Text = _pdfDoc.Pages[_pdfDoc.CurrentPage].Text;
        }
        
        void mPdfDoc_RenderNotifyFinished(int page, bool bSuccesss)
        {
            Invoke(new RenderNotifyInvoker(RenderNotifyFinished), page, bSuccesss);
        }


        #endregion






        public PdfViewer(Int32 dataid)
        {
            InitializeComponent();

            #region eventHandlers
            this.pdfViewBox.PictBox.MouseDown += new MouseEventHandler(PictBox_MouseDown);
            this.pdfViewBox.PictBox.MouseUp += new MouseEventHandler(PictBox_MouseUp);
            this.pdfViewBox.PictBox.MouseMove += new MouseEventHandler(pdfViewBox_MouseMove);
            
            this.hScrollBar.Scroll += new ScrollEventHandler(hScrollBar_Scroll);
            this.vScrollBar.Scroll += new ScrollEventHandler(vScrollBar_Scroll);
            #endregion
            this.Paint += new PaintEventHandler(PdfViewer_Paint);
            this.DataID = dataid;
            this.tPageViewer.View = View.Tile;
            this.handler = new DataBaseHandler();
           


        }

        
        
      
        // the new Constractor of the application
        public PdfViewer(DataBaseHandler handler)
        {
            
            InitializeComponent();
            this.handler = handler;
           

            #region eventHandlers
            this.pdfViewBox.PictBox.MouseDown += new MouseEventHandler(PictBox_MouseDown);
            this.pdfViewBox.PictBox.MouseUp += new MouseEventHandler(PictBox_MouseUp);
            this.pdfViewBox.PictBox.MouseMove += new MouseEventHandler(pdfViewBox_MouseMove);

            this.hScrollBar.Scroll += new ScrollEventHandler(hScrollBar_Scroll);
            this.vScrollBar.Scroll += new ScrollEventHandler(vScrollBar_Scroll);
            #endregion

            this.Paint += new PaintEventHandler(PdfViewer_Paint);
            this.tPageViewer.View = View.Tile;




        }


       
        private void PdfViewer_Load(object sender, EventArgs e)
        {
            
            try
            {
                fm = new TaskbarHide.FormState();
                fm.Maximize(this);               
              
                this.m_DataOptArray = this.handler.getDataOpt(this.DataID);
                this.dataHeaderBtn.Text = this.handler.getDataHeading(this.DataID);
                this.handler.getTxtData(this.DataID);
                this.pageNumber = 1;
                openPDF("temp.pdf");
                this.refreshPage();
                PDFPage pg = this.mPdfDoc.Pages[1];
                this.tPageViewer.TileSize = new Size(134, (int)(128 * pg.Height / pg.Width) + 10);
                this.tPageViewer.BeginUpdate();
                this.tPageViewer.Clear();
                for (int i = 0; i < this.mPdfDoc.PageCount; ++i)
                    this.tPageViewer.Items.Add((i + 1).ToString());
                this.tPageViewer.EndUpdate();
                this.TopMost = true;


            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);

            }
            
        }

        
        
        // the function that draws the items in the listview thumbril

        
        
        private void tPageViewer_DrawItem(object sender, DrawListViewItemEventArgs e)
        {

          
            
           
            if (this.mPdfDoc != null)
            {
                Rectangle bounds = e.Bounds;
                bounds.Offset(-1, -5);

                Rectangle shadowRect = new Rectangle(bounds.Location, bounds.Size);
                Rectangle pageRect = new Rectangle(bounds.Location, bounds.Size);

                int pgNum = e.ItemIndex + 1;
                PDFPage pg;
                if (!mPdfDoc.Pages.TryGetValue(pgNum, out pg))
                    return;
                pg.RenderThumbnailFinished -= new RenderNotifyFinishedHandler(pg_RenderThumbnailFinished);
                pg.RenderThumbnailFinished += new RenderNotifyFinishedHandler(pg_RenderThumbnailFinished);

                shadowRect.Offset(3, 3);
                shadowRect.Inflate(-6, -10);
                pageRect.Inflate(-6, -10);

                e.Graphics.FillRectangle(Brushes.LightGray, shadowRect);

                Bitmap bmp = pg.LoadThumbnail(128, (int)(128 * pg.Height / pg.Width));
                if (bmp != null)
                    e.Graphics.DrawImageUnscaledAndClipped(bmp, pageRect);

                e.Graphics.DrawRectangle(Pens.LightGray, pageRect);

                StringFormat stringFormat = new StringFormat();
                stringFormat.Alignment = StringAlignment.Center;
                stringFormat.LineAlignment = StringAlignment.Center;

                e.Graphics.DrawString(e.Item.Text, this.tPageViewer.Font, Brushes.Black, new RectangleF(e.Bounds.X, e.Bounds.Y + e.Bounds.Height - 10, e.Bounds.Width, 10), stringFormat);

                e.DrawFocusRectangle();
            }






        }

        private void exitBtn_Click(object sender, EventArgs e)
        {
            this.Close();


        }

        private void tPageViewer_MouseDown(object sender, MouseEventArgs e)
        {

            ListViewItem selection = this.tPageViewer.GetItemAt(e.X, e.Y);

            // If the user selects an item in the ListView, display
            // the image in the PictureBox.
            if (selection != null)
            {
                this.pageNumber = selection.Index + 1;
                this.refreshPage();
            }


        }

        
        private void mainTLPanel_Paint(object sender, PaintEventArgs e)
        {

            Point p = this.mainTLPanel.PointToScreen(new Point(this.mainTLPanel.ClientRectangle.X, this.mainTLPanel.ClientRectangle.Y));
            Rectangle screenRect = new Rectangle(p.X, p.Y, this.mainTLPanel.ClientRectangle.Width, this.mainTLPanel.ClientRectangle.Height);

            if (ApplicationCache.PdfWindowImage != null)
            {
                // Bitmap nBit = ((Bitmap)ApplicationCache.MainFormBGImage).Clone(tt, ApplicationCache.MainFormBGImage.PixelFormat);
                Graphics g = e.Graphics;

                g.DrawImage(ApplicationCache.MainFormBGImage, this.mainTLPanel.ClientRectangle,
                    screenRect.X, screenRect.Y, screenRect.Width, screenRect.Height, GraphicsUnit.Pixel);
                //nBit.Dispose();
            }






          

        }


        void PdfViewer_Paint(object sender, PaintEventArgs e)
        {

            Graphics g = e.Graphics;
            if (ApplicationCache.PdfWindowImage != null)
            {
                g.DrawImage(ApplicationCache.PdfWindowImage, new Rectangle(0, 0, this.Width, this.Height), 0, 0, this.Width, this.Height, GraphicsUnit.Pixel);


            }




        }







        //private void colLabel_Paint(object sender, PaintEventArgs e)
        //{

        //    Point p = this.colLabel.PointToScreen(new Point(this.colLabel.ClientRectangle.X, this.colLabel.ClientRectangle.Y));
        //    Rectangle screenRect = new Rectangle(p.X, p.Y, this.colLabel.ClientRectangle.Width, this.colLabel.ClientRectangle.Height);

        //    if (ApplicationCache.PdfWindowImage != null)
        //    {
        //        Graphics g = e.Graphics;
        //        g.DrawImage(ApplicationCache.PdfWindowImage, this.colLabel.ClientRectangle, screenRect.X, screenRect.Y, screenRect.Width, screenRect.Height, GraphicsUnit.Pixel);


        //    }


        //}

       

    }
}
